//
//  MPMoviePlayerVC.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/3.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "MPMoviePlayerVC.h"
#import "KJPlayerView.h"

@interface MPMoviePlayerVC () <KJPlayerViewDelegate>

@property (nonatomic, strong) KJPlayerView *MPPlayer;
@property (nonatomic, strong) UILabel *lbDetail;
@property (nonatomic, strong) UIButton *dianzanBtn;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UILabel *lbTitle;

@end

@implementation MPMoviePlayerVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [self.MPPlayer stop];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"视频";
    [self setupToPlay];
}

//拦截系统返回按钮点击事件
//- (BOOL)navigationShouldPopOnBackButton {
//
//    [self.MPPlayer stop];
//    return YES;
//}

- (void)setupToPlay{
    [self.view addSubview:self.MPPlayer];
   
    if ([PPNetworkHelper isWiFiNetwork]) {//实时网络
//        [self.MPPlayer play];
    } else {
        if ([UserDefaults boolForKey:UserDefaultsPlayInAllNet]) {//沙盒网络
//            [self.MPPlayer play];
        } else {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"您当前正在使用移动网络,\n继续播放将消耗流量" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * photo = [UIAlertAction actionWithTitle:@"继续播放" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                [self.MPPlayer play];
                [UserDefaults setBool:YES forKey:UserDefaultsPlayInAllNet];
                [UserDefaults synchronize];
            }];
            
            UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"停止播放" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            //添加各个按钮事件
            [alert addAction:photo];
            [alert addAction:cancel];
            //弹出sheet提示框
            [self presentViewController:alert animated:YES completion:nil];
        }
    }

    self.lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, GetViewYH(self.MPPlayer), SCREEN_WIDTH - 32, 120)];
    self.lbDetail = [[UILabel alloc] initWithFrame:CGRectMake(16, GetViewYH(self.lbTitle) + 12, 210, 17)];
    
    self.shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 16 - 40, GetViewYH(self.lbTitle) + 10, 40, 40)];
    self.dianzanBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 16 - 16 - 40 - 40, GetViewYH(self.lbTitle) + 10, 40, 40)];

    [self.shareBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    [self.dianzanBtn addTarget:self action:@selector(dianzan) forControlEvents:UIControlEventTouchUpInside];
    
    self.lbTitle.numberOfLines = 0;
    self.lbTitle.text = self.videoTitle;
    
    self.lbDetail.text = self.fromName;
    self.lbDetail.textColor = UIColorFromRGB(0x999999);
    self.lbDetail.font = FONT(12);
    
    [self.shareBtn setImage:ImageNamed(@"分享") forState:UIControlStateNormal];
    
    [self.dianzanBtn setImage:ImageNamed(@"点赞1") forState:UIControlStateNormal];
    self.dianzanBtn.hidden = YES;
    
    [self.view addSubview:self.lbTitle];
    [self.view addSubview:self.lbDetail];
    [self.view addSubview:self.shareBtn];
    [self.view addSubview:self.dianzanBtn];
}

- (void)share {

}

- (void)dianzan {

}

- (KJPlayerView *)MPPlayer {
    if (!_MPPlayer) {
        KJPlayerViewConfiguration *configuration = [[KJPlayerViewConfiguration alloc] init];
        configuration.autoHideTime = 5;
        configuration.playType = KJPlayerPlayTypeOnce;
        
        _MPPlayer = [[KJPlayerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * VideoScale) Configuration:configuration];
        _MPPlayer.backgroundColor = UIColor.blackColor;
        _MPPlayer.delegate = self;
        _MPPlayer.playerSuperView = self.view;
//        NSString *url = @"https://mp4.vjshi.com/2017-07-02/0cbbf21c6003f7936f4086dd10e7ebf5.mp4";
        NSString *url = DSStringValue(self.videoPath);
        [_MPPlayer kj_setPlayWithURL:url StartTime:0];
    }
    return _MPPlayer;
}



@end

